package com.bite.book.service;

import com.bite.book.dao.BookDao;
import com.bite.book.eunm.BookStatusEnum;
import com.bite.book.mapper.BookInfoMapper;
import com.bite.book.model.BookInfo;
import com.bite.book.model.PageRequest;
import com.bite.book.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BookService {
    @Autowired
    public BookInfoMapper bookInfoMapper;
    @Autowired
    public BookDao bookDao;

    public List<BookInfo> getList(){
        List<BookInfo> bookList = bookDao.mockBookList();
        for (BookInfo book : bookList){
            if(book.getStatus() == 1){
                book.setState("可借阅");
            }else {
                book.setState("不可借阅");
            }
        }
        return bookList;
    }

    public void addbook(BookInfo bookInfo) {
        bookInfoMapper.insertBook(bookInfo);
    }

    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest) {
        List<BookInfo> booklist = bookInfoMapper.selectByPage(pageRequest);
        Integer total = bookInfoMapper.count();
        for (BookInfo book : booklist){
            //System.out.println(book.toString());
            //对结果进行二次处理
            book.setState(BookStatusEnum.getBookStatus(book.getStatus()));
        }
        ResponseResult<BookInfo> ret = new ResponseResult<BookInfo>(total,booklist,pageRequest);
        return ret;
    }

    public BookInfo selectById(Integer id) {
        return bookInfoMapper.selectById(id);
    }

    public void updateById(BookInfo bookInfo) {
        System.out.println(1);
        bookInfoMapper.updateBookById(bookInfo);
    }

    public void deleteById(Integer id) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(0);
        bookInfoMapper.updateBookById(bookInfo);
    }

    public void batchDelete(List<Integer> ids) {
        bookInfoMapper.updateBatchById(ids);
    }

    public Integer selectByName(String bookName) {
        return bookInfoMapper.selectByName(bookName);
    }
}



